package com.ruoyi.business.customer.domain;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.business.base.JoBaseEntity;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.annotation.Sensitive;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.DesensitizedType;
import com.ruoyi.common.enums.RoleEnum;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.Data;
import lombok.Getter;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;

import java.util.Date;

/**
 * 客户对象 jb_customer
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Data
public class JbCustomer extends JoBaseEntity
{
    private static final long serialVersionUID = 1L;

    /**
     * 客户名称
     */
    @Excel(name = "客户名称")
    private String customerName;
    /**
     * 用友编码
     */
    @Excel(name = "用友编码")
    private String yyCode;
    /**
     * 客户名称拼音
     */
    @Excel(name = "客户名称拼音")
    private String customerNamePinyin;
    /**
     * 客户系统生成编码
     */
    @Excel(name = "客户系统生成编码")
    private String customerCode;

    /**
     * 来源渠道（抖音等）
     */
    @Excel(name = "来源渠道", readConverterExp = "抖=音等")
    private String opportunitySource;

    /**
     * 客户来源(线上线下)
     */
    @Excel(name = "商机渠道(线上线下)")
    private String opportunityChannel;

    /**
     * 客户类型（经销商，零售商和农户）
     */
    @Excel(name = "客户类型", readConverterExp = "经=销商，零售商和农户")
    private String customerType;

    /**
     * 尊称
     */
    @Excel(name = "尊称")
    private String customerTitle;

    /**
     * 手机号码
     */
    @Excel(name = "手机号码")
    // @Sensitive(desensitizedType = DesensitizedType.PHONE)
    private String phoneNumber;

    /**
     * 生日
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "生日", width = 30, dateFormat = "yyyy-MM-dd")
    private Date birthday;

    /**
     * 归属部门id
     */
    @Excel(name = "归属部门id")
    private Long departmentId;

    /**
     * 归属小组id
     */
    @Excel(name = "归属小组id")
    private Long teamId;

    /**
     * 所属业务经理(用户id)
     */
    @Excel(name = "所属业务经理(用户id)")
    private Long businessManagerId;

    /**
     * 前业务经理(用户id)
     */
    @Excel(name = "前业务经理(用户id)")
    private Long frontBusinessManagerId;

    /**
     * 客户状态（已成交，正在跟进）
     */
    @Excel(name = "客户状态", readConverterExp = "客户状态 来源字典")
    private String customerStatus;
    /**
     * 所在地区
     */
    @Excel(name = "所在省")
    private String localProvince;
    /**
     * 所在地区
     */
    @Excel(name = "所在市")
    private String localCity;
    /**
     * 所在地区
     */
    @Excel(name = "所在县")
    private String localCounty;

    /**
     * 所在地区
     */
    @Excel(name = "所在地区")
    private String localArea;
    /**
     * 所在地区
     */
    @Excel(name = "所在省")
    private String localProvinceName;
    /**
     * 所在地区
     */
    @Excel(name = "所在市")
    private String localCityName;
    /**
     * 所在地区
     */
    @Excel(name = "所在县")
    private String localCountyName;

    /**
     * 所在地区
     */
    @Excel(name = "所在地区")
    private String localAreaName;

    /**
     * 详细地址
     */
    @Excel(name = "详细地址")
    private String detailedAddress;

    /**
     * 客户星级
     */
    @Excel(name = "客户星级")
    private String customerStarLevel;

    /**
     * 客户标签
     */
    @Excel(name = "客户标签")
    private String customerLabels;

    /**
     * 痛点需求
     */
    @Excel(name = "痛点需求")
    private String customerNeed;

    /**
     * 统一社会信用代码
     */
    @Excel(name = "统一社会信用代码")
    private String creditCode;

    /**
     * 最新跟进时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "最新跟进时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date latestFollowupTime;

    /**
     * 门店图片URL
     */
    @Excel(name = "门店图片URL")
    private String storeImageUrl;

    /**
     * 商机id
     */
    @Excel(name = "商机id")
    private Long opportunityId;

    /**
     * 是否无效 0=正常1=无效
     */
    @Excel(name = "是否无效 0=正常1=无效")
    private Integer isInvalid;

    /**
     * 置顶 0最顶
     */
    @Excel(name = "置顶 0最顶")
    private Integer topUp;

    /**
     * 数据类型0=正常客户1=公司公海2=部门公海
     */
    @Excel(name = "数据类型0=正常客户1=公司公海2=部门公海3=无效客户")
    private Integer dataType;

    /**
     * 数据类型更新时间
     */
    @Excel(name = "数据类型变化更新时间")
    private Date dataTypeTime;

    /**
     * 最后转移客户的时间
     */
    @Excel(name = "最后转移客户的时间")
    private Date transferCustomerTime;

    /**
     * 备注
     */
    @Excel(name = "备注")
    private String remark;

    /**
     * 状态（0正常 1停用）
     */
    @Excel(name = "状态", readConverterExp = "0=正常,1=停用")
    private Long status;

    /**
     * 置顶时间
     */
    @Excel(name = "置顶时间")
    private Date topUpTime;
    public void setPhoneDesensitized(JbCustomer jbCustomer)
    {
        if (ObjUtil.isNotEmpty(jbCustomer)
            && StrUtil.isNotBlank(jbCustomer.getPhoneNumber())) {
            LoginUser securityUser = SecurityUtils.getLoginUser();
            // 管理员不脱敏
            boolean admin = securityUser.getUser().isAdmin();
            boolean ceo = SecurityUtils.hasUserRole(RoleEnum.CEO.getCode());
            boolean adminRole = SecurityUtils.hasUserRole(RoleEnum.ADMIN.getCode());
            Long createBy = jbCustomer.getCreateBy();
            Long businessManagerId1 = jbCustomer.getBusinessManagerId();

            if (!admin && !ceo && !adminRole
                    && (!securityUser.getUser().getUserId().equals(createBy)
                    && !securityUser.getUser().getUserId().equals(businessManagerId1))) {
                DesensitizedType desensitizedType = DesensitizedType.PHONE;
                String phone = desensitizedType.desensitizer().apply(jbCustomer.getPhoneNumber());
                jbCustomer.setPhoneNumber(phone);
            }
        }
    }
}
